"""Gene Set Collection

.. helpdoc::
Generate a gene set collection to be used to test candidate gene sets for enrichment.  This tool takes an expression set of data from other Affy or related widgets and sends a Gene Set signal that can be used to calculate enrichment values along with a vector of test statistics (done in other widgets).
"""

"""<widgetXML>
    <name>Gene Set Collection</name>
    <icon>default.png</icon>
    <tags>
        <tag>Affy</tag>
    </tags>
    <summary>Generates a gene set collection.</summary>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

from OWRpy import * 
import redRGUI
import signals
class gsc(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["genefilter", "GSEABase"])
        self.setRvariableNames(["gsc", "tempEset"])
        self.RFunctionParam_abatch = ''
        
        """.. rrsignals::
        .. helpdoc::
        Input is an Eset containing expression and annotation data, use other tools to generate or read expression data into this format.
        """
        self.inputs.addInput('ab', "Affy Batch", signals.affy.REset, self.processabatch)
        
        
        """.. rrsignals::"""
        self.outputs.addOutput('gsc', "Gene Set Collection", signals.affy.GSC)
        
        self.GUIParam_nsFilter = redRGUI.base.TFCheckBox(self.controlArea, label = "Filter Not Significant", setChecked = True, toolTip = "Perform filtering of probes for those in the set that have high variance and high levels of expression.")
        
        self.GUIParam_varCutoff = redRGUI.base.spinBox(self.controlArea, label = "Variance Cutoff", min = 0, max = 1, value = 0.5, decimal = 3, toolTip = "Cutoff for Not Significance filtering, this is only used if Filter Not Significanct is checked.")
        
        self.GUIParam_collectionType = redRGUI.base.radioButtons(self.controlArea, label = "Collection Type", buttons = [('GOCollection()', "GO"), ('KEGGCollection()', "KEGG"), ('ChrCollection()', "Chr"), ('ChrlocCollection()', 'Chrloc'), ('OMIMCollection()', "OMIM"), ('PMIDCollection()', "PMID")], setChecked = "KEGGCollection()", toolTip = "Type of collection that will be used to test gene set enrichment")
        
        self.GUIParam_minTerms = redRGUI.base.spinBox(self.controlArea, label = "Min Gene In Set", min = 1, value = 10, decimal = 0, toolTip = "Minimum number of genes that a set must contain to be considered for set enrichment.")
        
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processabatch(self, data):
        if data:
            self.RFunctionParam_abatch=data.getData()
            
        else:
            self.RFunctionParam_abatch=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_abatch) == '': return
        d = {'GSC':self.RVariables['gsc'], 'DATA':self.RFunctionParam_abatch, 'VC':unicode(self.GUIParam_varCutoff.value()), 'TEMP':self.RVariables['tempEset'], 'SET':self.GUIParam_collectionType.checkedId()}
        self.R('%(TEMP)s<-%(DATA)s' % d, wantType = redR.NOCONVERSION)
        if self.GUIParam_nsFilter.checked() == "TRUE":
            self.R('%(TEMP)s<-nsFilter(%(TEMP)s, var.cutoff = %(VC)s)$eset' % d, wantType = redR.NOCONVERSION)
        self.R('%(GSC)s<-GeneSetCollection(%(TEMP)s, setType = %(SET)s)' % d, wantType = redR.NOCONVERSION)
        newData = signals.affy.GSC(self, data = d['GSC']) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        self.rSend("gsc", newData)
